package com.lvyq.javaprint.util;

import com.lvyq.javaprint.model.ProxySettingModel;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.*;

/**
 * @author lvyq 打印控件设置查询修改-目前只有代理设置，暂时写在一个文件中，后期如果有其他设置，再进行拆分
 * @version 1.0
 * Create by 2025-01-06 16:36
 */
public class SettingUtils {

    /**
     * @description 读取代理设置
     * @author lvyq
     * @throws
     * @return ProxySetting
     * @time 2025-01-06 16:53
     */
    public static ProxySettingModel readProxySetting() {
        ProxySettingModel setting = new ProxySettingModel();
        try {
            String filePath=System.getProperty("user.dir")+"\\setting.xml";
            // 读取xml文件
            Document doc= null;
            doc = new SAXReader().read(new File(filePath));
            // 获取根节点
            Element rootElement = doc.getRootElement();
            // 获取代理设置
            Element proxyElem= rootElement.element("proxy");
            Element content = proxyElem.element("content");
            //代理类型  1 不使用代理 2 网页代理(HTTP)
            String type = content.attribute("type").getValue();
            String domain = content.attribute("domain").getValue();
            String port =content.attribute("port").getValue();
            setting.setType(type);
            setting.setDomain(domain);
            setting.setPort(port);
        } catch (DocumentException e) {
            //异常时，默认不启用
            setting.setType("1");
            throw new RuntimeException(e);
        }

        return setting;
    }



    /**
     * @description 设置代理
     * @author lvyq
     * @throws
     * @return ProxySetting
     * @time 2025-01-06 17:06
     */
    public static void writeProxySetting(ProxySettingModel proxySetting) throws DocumentException, IOException {
        String filePath=System.getProperty("user.dir")+"\\setting.xml";
        // 读取xml文件
        Document doc=new SAXReader().read(new File(filePath));
        // 获取根节点
        Element rootElement = doc.getRootElement();
        // 获取代理设置
        Element proxyElem= rootElement.element("proxy");
        Element content = proxyElem.element("content");
        //代理类型  1 不使用代理 2 网页代理(HTTP)
        content.addAttribute("type",proxySetting.getType());
        content.addAttribute("domain",proxySetting.getDomain());
        content.addAttribute("port",proxySetting.getPort());
        FileOutputStream out =new FileOutputStream(filePath);
        OutputFormat format= OutputFormat.createPrettyPrint();
        XMLWriter writer=new XMLWriter(out,format);
        writer.write(doc);
        writer.close();
    }

    /**
     * @description 读取背景图设置
     * @author lvyq
     * @throws
     * @return String
     * @time 2025-01-10 17:19
     */
    public static String readBackImgPath() {
        String backImgPath="";
        ProxySettingModel setting = new ProxySettingModel();
        try {
            String filePath=System.getProperty("user.dir")+"\\setting.xml";
            // 读取xml文件
            Document doc= null;
            doc = new SAXReader().read(new File(filePath));
            // 获取根节点
            Element rootElement = doc.getRootElement();
            Element proxyElem= rootElement.element("backimg");
            Element content = proxyElem.element("content");
            backImgPath = content.attribute("path").getValue();
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
        return backImgPath;
    }

    /**
     * @description 存储背景图地址
     * @author lvyq
     * @param[1] path
     * @throws
     * @time 2025-01-10 17:20
     */
    public static void writeBackImgPath(String path) throws DocumentException, IOException {
        String filePath=System.getProperty("user.dir")+"\\setting.xml";
        // 读取xml文件
        Document doc=new SAXReader().read(new File(filePath));
        // 获取根节点
        Element rootElement = doc.getRootElement();
        Element proxyElem= rootElement.element("backimg");
        Element content = proxyElem.element("content");
        content.addAttribute("path",path);
        FileOutputStream out =new FileOutputStream(filePath);
        OutputFormat format= OutputFormat.createPrettyPrint();
        XMLWriter writer=new XMLWriter(out,format);
        writer.write(doc);
        writer.close();
    }

    /**
     * @description 获取背景图
     * @author lvyq
     * @throws
     * @return ImageView
     * @time 2025-01-13 8:41
     */
    public static ImageView getBackGroundImg(){
        //背景图
        ImageView backGroundImg= new ImageView();
        backGroundImg.setSmooth(true);
        backGroundImg.setFitWidth(50);
        String imgPath = SettingUtils.readBackImgPath();
        if (imgPath!=null && !"".equals(imgPath)){
            Image image = new Image(imgPath);
            backGroundImg= new ImageView(image);
        }
        return backGroundImg;
    }

    /**
     * @description 设置端口号
     * @author lvyq
     * @throws
     * @time 2025-07-24 11:49
     */
    public static void  writeServerPort(String serverPort) throws IOException, DocumentException {
        String filePath=System.getProperty("user.dir")+"\\setting.xml";
        // 读取xml文件
        Document doc=new SAXReader().read(new File(filePath));
        // 获取根节点
        Element rootElement = doc.getRootElement();
        Element proxyElem= rootElement.element("serverport");
        Element content = proxyElem.element("content");
        content.addAttribute("port",serverPort);
        FileOutputStream out =new FileOutputStream(filePath);
        OutputFormat format= OutputFormat.createPrettyPrint();
        XMLWriter writer=new XMLWriter(out,format);
        writer.write(doc);
        writer.close();
    }

    /**
     * @description 获取端口号
     * @author lvyq
     * @throws
     * @return String
     * @time 2025-07-24 11:49
     */
    public static String  readServerPort(){
        String serverPort="";
        ProxySettingModel setting = new ProxySettingModel();
        try {
            String filePath=System.getProperty("user.dir")+"\\setting.xml";
            // 读取xml文件
            Document doc= null;
            doc = new SAXReader().read(new File(filePath));
            // 获取根节点
            Element rootElement = doc.getRootElement();
            Element proxyElem= rootElement.element("serverport");
            Element content = proxyElem.element("content");
            serverPort = content.attribute("port").getValue();
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
        return serverPort;
    }
}
